package cn.itcast.usermanage.controller;



import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.alibaba.druid.util.StringUtils;
import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;

@RequestMapping("rest/user")
@Controller
public class UserController2 {
	@Autowired
	private UserService userService;
	
	/**
	 * 根据ID查询用户
	 */
	@GetMapping(value="{id}")
	public ResponseEntity<User> queryUserById(@PathVariable("id")Long id){
		try {
			if (id.longValue() < 1) {
				//400
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
			}
			User user = this.userService.queryUserById(id);
			if (user == null) {
				//资源不存在404
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			return ResponseEntity.status(HttpStatus.OK).body(user);//200
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	/**
	 * 新增用户
	 */
	@PostMapping()
	public ResponseEntity<Void>saveUser(User user){
		try {
			if (StringUtils.isEmpty(user.getUserName())) {
				//400
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			this.userService.saveUser(user);
			return ResponseEntity.status(HttpStatus.CREATED).build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	/**
	 * 更新数据
	 */
	@PutMapping()
	public ResponseEntity<Void>updateUser(User user){
		try {
			if (user.getId() == null || user.getId() < 1) {
				//400
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			this.userService.updateUser(user);
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	/**
	 * 删除用户数据
	 */
	@DeleteMapping
	public ResponseEntity<Void> deleteUser(@RequestParam("ids")List<Object> ids){
		try {
			if(ids == null){
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			Boolean b = this.userService.deleteUser(ids);
			if(b){
				return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
}